##client.py
from socket import *
from sys import *
from time import *
from monocipher import *
from polycipher import *
from homophonic import *
from polygram import *
import random

if len(argv) < 4:
    print("error: run 'python client.py hostname inputfile mode'")
    exit(0)

random.seed(5)

HOST = argv[1]
FILE = argv[2]
PORT = 8005
SEQ = random.randint(1000000, 2000000)

if argv[3] == '0':
    cipher = MonoCipher()
elif argv[3] == '1':
    cipher = PolyCipher()
elif argv[3] == '2':
    cipher = HomoPhonicCipher()
elif argv[3] == '3':
    cipher = PolygramCipher('4')
else:
    print 'error: cyphermode argument not recognized'
    exit(0)

while True:
    file = open(FILE, 'r')

    client = socket(AF_INET,SOCK_STREAM)
    client.connect((HOST, PORT))
    print 'connected: ' + HOST

    for line in file:
        message = line.strip()
    
        if argv[3] != '0':
            encrypted = str(SEQ) + cipher.encrypt(message)
        else:
            encrypted = cipher.encrypt(str(SEQ) + message)
    
        print 'sending: ' + str(SEQ) + message
        print 'encrypted: ' + encrypted
        print ''
    
        client.send(encrypted)
        SEQ = random.randint(1000000, 2000000)
        sleep(60) #Set to 2 for testing, should be sleep(60)

    client.close()
